package com.example.lending.ViewModel.Repository;

import android.content.Context;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import com.example.lending.Model.Dao.OperationDao;
import com.example.lending.Model.Database;
import com.example.lending.Model.Operation;

import java.util.List;

public class OperationRepository {
    private LiveData<List<Operation>> allOperationLive;
    private OperationDao operationDao;
    public LiveData<List<Operation>> getAllOperationLive(){return allOperationLive;}
    public OperationRepository(Context context){
        Database operationDatabase = Database.getDatabase(context.getApplicationContext());
        operationDao=operationDatabase.getOperationDao();
        allOperationLive = operationDao.getAllOperationLive();
    }
    public void insertOperations(Operation... operations){
        new OperationRepository.InsertAsyncTask(operationDao).execute(operations);
    }
    public void updataOperations(Operation... operations){
        new OperationRepository.UpdataAsyncTask(operationDao).execute(operations);
    }
    public void deleteOperations(Operation... operations){
        new OperationRepository.InsertAsyncTask(operationDao).execute(operations);
    }
    public void deleteAllOperations(Void... voids){
        new OperationRepository.DeleteAllAsyncTask(operationDao).execute();
    }

    static class InsertAsyncTask extends AsyncTask<Operation,Void,Void> {
        private OperationDao operationDao;
        public InsertAsyncTask(OperationDao operationDao) {
            this.operationDao = operationDao;
        }

        @Override
        protected Void doInBackground(Operation... operations) {
            operationDao.insertOperation(operations);
            return null;
        }
    }
    static class UpdataAsyncTask extends AsyncTask<Operation,Void,Void>{
        private OperationDao operationDao;
        public UpdataAsyncTask(OperationDao operationDao) {
            this.operationDao = operationDao;
        }

        @Override
        protected Void doInBackground(Operation... operations) {
            operationDao.updataOperation(operations);
            return null;
        }
    }
    static class DeleteAsyncTask extends AsyncTask<Operation,Void,Void>{
        private OperationDao operationDao;
        public DeleteAsyncTask(OperationDao operationDao) {
            this.operationDao = operationDao;
        }

        @Override
        protected Void doInBackground(Operation... operations) {
            operationDao.deleteOperation(operations);
            return null;
        }
    }
    static class DeleteAllAsyncTask extends AsyncTask<Void,Void,Void>{
        private OperationDao operationDao;
        public DeleteAllAsyncTask(OperationDao operationDao) {
            this.operationDao = operationDao;
        }

        @Override
        protected Void doInBackground(Void... voids) {
            operationDao.deleteAllOperation();
            return null;
        }
    }
}
